﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CommDll;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace InstitutionAssitant
{
    public partial class frmPharmacySupervision : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性        
        public DataTable m_dt;  // 保存查询出来的数据
        private string m_strSql;    // 查询语句
        public DataTable m_dt_fy;
        #endregion
        public frmPharmacySupervision()
        {
            InitializeComponent();
        }
        #region 02.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init()
        {
            // 01 dataGridViewX1设为不可编辑
            dataGridViewX1.ReadOnly = true;
            dataGridViewX1.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
            dataGridViewX2.ReadOnly = true;
            dataGridViewX2.Anchor = (System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Top));
            // 02 DataGridView
            _initDgvTitle();
            panel3.Width = Convert.ToInt32(this.Width * 60 / 100);
            panel4.Width = Convert.ToInt32(this.Width * 40 / 100);
        }
        /// <summary>
        /// 初始化DataGridView字段显示信息
        /// </summary>
        public void _initDgvTitle()
        {
            string strInfo = "";
            string strKind = "";
            // 01 frmLocalOrder
            strKind = this.Name;
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strKind))
            {
                return;
            }
            strInfo = "InstitutionCode-机构编号,Drungcode-溯源码,Code_His-编号,Name-商品名称,NameSpell-商品规格,WarehouseUnit-单位,ApprovalNumber-批准文号,SupplierName_His-供应商名称,Manufacturer-生产厂家,MakeDate-生产日期,ExpiredDate-有效截止日期,BatchCode-批次号,StockInTime-入库时间,StockInCode_His-入库单号";
            AppConfig.m_dictFieldName.Add(strKind, strInfo);
            strInfo = "InstitutionCode,Drungcode,Code_His,Name,NameSpell,WarehouseUnit,ApprovalNumber,SupplierName_His,Manufacturer,MakeDate,ExpiredDate,BatchCode,StockInTime,StockInCode_His";
            AppConfig.m_dictFieldSql.Add(strKind, strInfo);
            // 与SqlField同
            AppConfig.m_dictField0.Add(strKind, strInfo);
            AppConfig.m_dictField.Add(strKind, strInfo);
            strInfo = "0,180,100,120,80,80,120,160,160,80,80,80,120,120";
            AppConfig.m_dictFieldWidth0.Add(strKind, strInfo);
            AppConfig.m_dictFieldWidth.Add(strKind, strInfo);

            strKind = this.Name + "1";
            // 存在时不再处理
            if (AppConfig.m_dictFieldName.ContainsKey(strKind))
            {
                return;
            }
            strInfo = "drug_trac_codg-溯源码,psn_name-患者姓名,certno-证件号码,rxno-处方单号,sel_retn_cnt-发药数量,sel_retn_time-发药时间,returnTime-退药时间";
            AppConfig.m_dictFieldName.Add(strKind, strInfo);
            strInfo = "drug_trac_codg,psn_name,certno,rxno,sel_retn_cnt,sel_retn_time,returnTime";
            AppConfig.m_dictFieldSql.Add(strKind, strInfo);            
            AppConfig.m_dictField0.Add(strKind, strInfo);
            AppConfig.m_dictField.Add(strKind, strInfo);
            strInfo = "150,200,200";
            AppConfig.m_dictFieldWidth0.Add(strKind, strInfo);
            AppConfig.m_dictFieldWidth.Add(strKind, strInfo);
            
        }
        /// <summary>
        /// 信息查询
        /// </summary>
        private void _query()
        {
            if (m_dt != null)
            {
                m_dt.Dispose();
            }
            if (m_dt_fy != null)
            {
                m_dt_fy.Dispose();
            }
            // 00 取条件:有参数时按参数来取数据
            if (string.IsNullOrEmpty(txtSupervisionCode.Text.Trim()))
            {
                CommFun._messageBoxShow("要查询的溯源码不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string _supervisioncode = txtSupervisionCode.Text.Trim();
            string _goodsname = "";
            // 02 商品名称
            if (cbGoodsName.Checked)
            {
                _goodsname = txtGoodsName.Text.Trim();
            }
            string strWhere = "";
            strWhere = "InstitutionCode='{0}'";
            strWhere = string.Format(strWhere, AppInfo.m_UserInfo.m_strDeptCode);
            string strSql = string.Format("exec NP_SearchStockBuDrungcode '{0}','{1}'", _supervisioncode, _goodsname);
            string strSqlfy= string.Format("exec NP_SearchSaleByDrungcode '{0}'", _supervisioncode);
            m_dt = DbHelper.DBAccess[0]._openSql_Dt(strSql);
            m_dt_fy = DbHelper.DBAccess[0]._openSql_Dt(strSqlfy);
            m_strSql = strSql;  // 更新数据用
            
            // 03 显示数据
            DataTable dt = null;
            DataTable dt_fy = null;
            if (m_dt != null)
            {
                dt = m_dt.Copy();
                //DataView dv = dt.DefaultView;
                //dv.RowFilter = strWhere;
            }
            if (m_dt_fy != null)
                dt_fy = m_dt_fy.Copy();
            _showData(dt);
            _showData_fy(dt_fy);
        }

        /// <summary>
        /// 数据显示
        /// </summary>
        /// <param name="dt"></param>
        private void _showData(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                // 0101 调整字段顺序
                CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[this.Name]);
                // 0102 显示数据
                CommFun._showDataGridView0(dataGridViewX1, dt, AppConfig.m_dictFieldWidth[this.Name], AppConfig.m_dictFieldName[this.Name], false, false);                
            }
            else
            {
                // 清除数据
                DataTable dtTemp = (DataTable)dataGridViewX1.DataSource;
                if (dtTemp != null && dtTemp.Rows.Count > 0)
                {
                    dtTemp.Rows.Clear();
                }
            }
            _GetTotalInfo();
        }
        private void _showData_fy(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                // 0101 调整字段顺序
                CommFun._dataTableFieldMove(dt, AppConfig.m_dictField[this.Name + "1"]);
                // 0102 显示数据
                CommFun._showDataGridView0(dataGridViewX2, dt, AppConfig.m_dictFieldWidth[this.Name + "1"], AppConfig.m_dictFieldName[this.Name + "1"], false, false);
            }
            else
            {
                // 清除数据
                DataTable dtTemp = (DataTable)dataGridViewX2.DataSource;
                if (dtTemp != null && dtTemp.Rows.Count > 0)
                {
                    dtTemp.Rows.Clear();
                }
            }            
        }
        
        /// <summary>
        /// 取汇总信息
        /// </summary>
        private void _GetTotalInfo()
        {
            lblRecord.Text = string.Format("提示:共有数据信息 {0} 项", dataGridViewX1 == null ? 0 : dataGridViewX1.Rows.Count);
        }
        #endregion

        #region 03.系统事件方法
        private void buttonX1_Click(object sender, EventArgs e)
        {
            // 数据查询
            _query();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            // 数据导出
            if (dataGridViewX1 != null && dataGridViewX2.Rows.Count > 0)
            {
                CommFun._ExportExcel("", dataGridViewX1);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void frmPharmacySupervision_Load(object sender, EventArgs e)
        {
            _init();
        }

        #endregion

        private void buttonX2_Click(object sender, EventArgs e)
        {
            // 数据导出
            if (dataGridViewX1 != null && dataGridViewX1.Rows.Count > 0)
            {
                CommFun._ExportExcel("", dataGridViewX1);
            }
        }
    }
}
